package calorie.com.calorie.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.CountDownTimerUtils;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.RegexUtils;
import com.lxh.baselibray.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;
import calorie.com.calorie.R;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.FirstLoginModifyPwdContact;
import calorie.com.calorie.request.BaseResponce;
import retrofit2.Response;

/**
 * 第次登录修改密码
 */
public class FirstLoginModifyPwdActivity extends MvpActivity<FirstLoginModifyPwdContact.FisrtLoginModifyPwdPResent> implements FirstLoginModifyPwdContact.IFirstLoginModifyPwdView {
    @BindView(R.id.et_login_pwd)
    EditText etLoginPwd;
    @BindView(R.id.et_confirm_login_pwd)
    EditText etConfirmLoginPwd;
    @BindView(R.id.et_deal_pwd)
    EditText etDealPwd;
    @BindView(R.id.et_confirm_deal_pwd)
    EditText etConfirmDealPwd;
    @BindView(R.id.et_tel)
    EditText etTel;
    @BindView(R.id.et_security_code)
    EditText etSecurityCode;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;


    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_first_login_modify_pwd;
    }


    @OnClick({R.id.tv_get_code, R.id.btn_confirm})
    public void onViewClicked(View view) {
        String tel = etTel.getText().toString();
        switch (view.getId()) {
            case R.id.tv_get_code:
                if (ObjectUtils.isEmpty(tel)) {
                    ToastUtils.showLongToast(context, "手机号不能为空");
                    return;
                }
                if (!RegexUtils.isMobileExact(tel)) {
                    ToastUtils.showLongToast(context, "请输入正确的手机号");
                    return;
                }
                mPresent.getSecurityCode(context,tel);
                break;
            case R.id.btn_confirm:
                String securityCode = etSecurityCode.getText().toString();
                String logPwd = etLoginPwd.getText().toString();
                String confirmLoginPwd = etConfirmLoginPwd.getText().toString();
                String dealPwd = etDealPwd.getText().toString();
                String confirmDealPwd = etConfirmDealPwd.getText().toString();

                if (validateRegister(tel, securityCode, logPwd, confirmLoginPwd,  dealPwd, confirmDealPwd))
                    return;
                showLoadingDialog();
                mPresent.modifyPwd(context,logPwd,dealPwd,tel,securityCode);
                break;

        }
    }




    private boolean validateRegister(String tel, String securityCode, String logPwd, String confirmLoginPwd,  String dealPwd, String confirmDealPwd) {
        if (ObjectUtils.isEmpty(logPwd)) {
            ToastUtils.showLongToast(context, "登录密码不能为空");
            return true;
        }
        if (ObjectUtils.isEmpty(confirmLoginPwd)) {
            ToastUtils.showLongToast(context, "确认登录密码不能为空");
            return true;
        }
        if (!ObjectUtils.equals(logPwd, confirmLoginPwd)) {
            ToastUtils.showLongToast(context, "登录密码不一致");
            return true;
        }
        if (!RegexUtils.isLoginPwd(logPwd)){
            ToastUtils.showLongToast(context,getString(R.string.login_pwd_regex));
            return true;
        }
        if (ObjectUtils.isEmpty(dealPwd)) {
            ToastUtils.showLongToast(context, "交易密码不能为空");
            return true;
        }
        if (ObjectUtils.isEmpty(confirmDealPwd)) {
            ToastUtils.showLongToast(context, "确认交易密码不能为空");
            return true;
        }
        if (!ObjectUtils.equals(dealPwd, confirmDealPwd)) {
            ToastUtils.showLongToast(context, "交易密码不一致");
            return true;
        }
        if (ObjectUtils.isEmpty(tel)) {
            ToastUtils.showLongToast(context, "手机号不能为空");
            return true;
        }
        if (!RegexUtils.isMobileExact(tel)) {
            ToastUtils.showLongToast(context, "请输入正确的手机号");
            return true;
        }
        if (ObjectUtils.isEmpty(securityCode)) {
            ToastUtils.showLongToast(context, "验证码不能为空");
            return true;
        }
        return false;
    }

    @Override
    public void getSecurityCode(Response<BaseResponce> response) {
        if (response.body().getCode()== Constant.SUCCESS_CODE){
            ToastUtils.showLongToast(context, "已发送验证码");
            CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(tvGetCode, 60000, 1000);
            countDownTimerUtils.start();
        }else {
            ToastUtils.showLongToast(context,response.body().getMsg());
        }
    }

    @Override
    public void getModifyPwdData(Response<BaseResponce> response) {
        dismissLoadingDialog();
        if (response.body().getCode()==Constant.SUCCESS_CODE){
            ToastUtils.showLongToast(context,"修改密码成功,去登录");
            startActivity(new Intent(context,LoginActivity.class));
            finish();
        }else {
            ToastUtils.showLongToast(context,response.body().getMsg());
        }
    }

    @Override
    protected FirstLoginModifyPwdContact.FisrtLoginModifyPwdPResent createPresent() {
        return new FirstLoginModifyPwdContact.FisrtLoginModifyPwdPResent();
    }
}
